import com.thelab.ui.*;

dynamic class com.thelab.ui.Slider extends UIElement
{	
	var _track:Track;
	var _slider:Dot;	
	var _step0:Dot;
	var _step1:Dot;
	var _direction:String;
	var _step:Number;
	private var __ratio:Number;
	
	var onStep:Function;	
	var onClick:Function;
	var onSlide:Function;
	
	public function Slider(scope:MovieClip,px:Number,py:Number,size:Number,direction:String)
	{
		super("UISlider",scope);
		var st:UIStyle;
		target._x = px || 0;
		target._y = py || 0;	
		__ratio = 0;
		_size = size;
		_step = 2;
		_direction = direction || "right";
		trace(arguments);
		//trace(typeof(scope)+" "+px+" "+py+" "+size+" "+direction);
		
		
		clearEvents();			
		
		_track = new Track(target,0,0,size,direction);
		_track.onClick = ontrackclick;
		st = _track._style;		
		st.linethick = 2;
		st.linealpha = 100;
		st.linecolor = 0x666666;
		_track._style = st;
		
		_step0 = new Dot(target,0,0,3);		
		_step0.onClick = _step0.onHold = onstep0;
		_step0._dclickTime = -1;
		st = _step0._style;
		st.linealpha = 0;
		st.backcolor = 0x666666;
		_step0._style = st;
		switch(_direction)
		{
			case "up":   { _step1 = new Dot(target,0,-size,3); break;	}
			case "down": { _step1 = new Dot(target,0,size,3);  break;	}
			case "left": { _step1 = new Dot(target,-size,0,3); break;	}
			case "right":{ _step1 = new Dot(target,size,0,3);  break;	}
		}
		
		_step1.onClick = _step1.onHold = onstep1;
		_step1._dclickTime = -1;
		_step1._style = st;
		
		_slider = new Dot(target,0,0,5);
		_slider.onDrag = onslide;
		_slider._fixed = false;		
		
		draw();		
	}
	
	function get _ratio():Number {	return __ratio; }
	function set _ratio(v:Number)
	{
		
		v = (v<0)?0:((v>1.0)?1.0:v);	
		
		var base = this;
		var s:Number = base.style.size;		
		switch(_direction)
		{
			case "up":   { _slider._y = -v*s; break;	}
			case "down": { _slider._y = v*s;  break;	}
			case "left": { _slider._x = -v*s; break;	}
			case "right":{ _slider._x = v*s;  break;	}
		}
		__ratio = v;
	}
	private function ontrackclick(track:Track,ratio:Number)
	{			
		var base = track;		
		base = base.parent.__base;		
		base._ratio = ratio;		
		base.onClick(base,ratio);
	}
	
	private function onslide(ref:Dot)
	{
		
		var base = this;
		var parent = base.parent.__base;
		var px:Number = ref._x;
		var py:Number = ref._y;
		var s:Number = parent.style.size;
		
		switch(parent._direction)
		{
			case "up":   { ref._x=0; ref._y = (py<=-s)?-s:((py>=0)?0:py);parent.__ratio = ref._y/s; break;	}
			case "down": { ref._x=0; ref._y = (py>=s)?s:((py<=0)?0:py); parent.__ratio = ref._y/s;break;	}
			case "left": { ref._y=0; ref._x = (px<=-s)?-s:((px>=0)?0:px);parent.__ratio = ref._x/s; break;	}
			case "right":{ ref._y=0; ref._x = (px>=s)?s:((px<=0)?0:px);parent.__ratio = ref._x/s; break;	}
		}			
		
		parent.onSlide(parent,parent._ratio);
	}
	
	private function onstep0(ref:Dot)
	{
		var base = this;
		var parent = base.parent.__base;
		var move_ratio:Number = parent._step/parent.style.size;
		var r:Number = parent._ratio;
		
		switch(parent._direction)
		{
			case "up":   { parent.onStep(parent,"down",parent._ratio); parent._ratio=r-move_ratio; break;	}
			case "down": { parent.onStep(parent,"up",parent._ratio);   parent._ratio=r-move_ratio;break;	}
			case "left": { parent.onStep(parent,"right",parent._ratio);parent._ratio=r-move_ratio; break;	}
			case "right":{ parent.onStep(parent,"left",parent._ratio); parent._ratio=r-move_ratio;break;	}
		}		
	}
	private function onstep1(ref:Dot)
	{
		var base = this;
		var parent = base.parent.__base;
		var move_ratio:Number = parent._step/parent.style.size;
		var r:Number = parent._ratio;
		
		switch(parent._direction)
		{
			case "up":   { parent._ratio=(r+move_ratio); break;	}
			case "down": { parent._ratio=(r+move_ratio); break;	}
			case "left": { parent._ratio=(r+move_ratio); break;	}
			case "right":{ parent._ratio=(r+move_ratio); break;	}
		}	
		
		parent.onStep(parent,parent._direction,parent._ratio);
	}
	
	static function getValue(min:Number,max:Number,ratio:Number)
	{
		var i:Number = (min<max)?min:max;
		var f:Number = (min>max)?min:max;
		return i+ (f-i)*ratio;
	}
	
	function draw()
	{
		_track.draw();
		_slider.draw();
		_step0.draw();
		_step1.draw();
	}
}
